package io.gitee.yxsnake.wechat.model.exception;

import io.gitee.yxsnake.embark.web.core.base.IResultCode;
import io.gitee.yxsnake.embark.web.core.context.TrackContext;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Optional;

/**
 * @author snake
 * @description
 * @since 2024/10/24 0:31
 */
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("all")
public enum WechatErrorEnum implements IResultCode, Serializable {


    INTERNAL_SERVER_ERROR(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"微信开放平台调用未知的异常"),
    ;

    private Integer code;

    private String msg;


    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    public String getTrackId() {
        return TrackContext.getTrackId();
    }


    @Override
    public String toString() {
        return "{" +
                "\"code\":\"" + code + '\"' +
                ", \"msg\":\"" + msg + '\"' +
                '}';
    }


    public static WechatErrorEnum getValue(String code) {
        Optional<WechatErrorEnum> optionalWechatErrorEnum = Arrays.asList(values())
                .stream()
                .filter(item -> item.getCode().equals(code)).findFirst();
        if(optionalWechatErrorEnum.isPresent()){
            return optionalWechatErrorEnum.get();
        }
        // 默认系统执行错误
        return INTERNAL_SERVER_ERROR;
    }
}
